package com.fashion.web.filter;

import net.sf.ehcache.constructs.web.AlreadyGzippedException;
import net.sf.ehcache.constructs.web.GenericResponseWrapper;
import net.sf.ehcache.constructs.web.PageInfo;
import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * User: Administrator
 * Date: 12-3-31
 * Time: 下午5:07
 */
public class UtfCacheFilter extends SimplePageCachingFilter {
    @Override
    protected PageInfo buildPage(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws AlreadyGzippedException, IOException, ServletException {
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        final ResponseWrapper wrapper = new ResponseWrapper(response, outputStream);
        chain.doFilter(request, wrapper);
        wrapper.flush();

        // Return the page info
        return new PageInfo(wrapper.getStatus(), wrapper.getContentType(), wrapper.getHeaders(), wrapper.getCookies(),
                outputStream.toByteArray(), true);
    }
}
